<?php
	class Version_db extends CI_Model {
		public function __construct() {
			$this->load->database();
		}
		
		public function current() {
			$sql = "SELECT * FROM version WHERE is_current=1";
			$query = $this->db->query($sql, array($ID));
			return $query;	
		}

		public function add($versionName, $versionDescription) {
			$sql = "INSERT INTO version (name, description, is_current, is_delete) VALUES (?, ?, ?, ?)";
			$query = $this->db->query($sql, array($versionName, $versionDescription, 0, 0));

			$res = $this->db->query("SELECT LAST_INSERT_ID()")->row_array();
			$id = $res['LAST_INSERT_ID()'];

			for ($i = 1; $i <= 4; $i++) {
				$sql = "INSERT INTO result VALUES (?, ?, -1, -1, -1, -1, -1)";
				$this->db->query($sql, array($id, $i));
			}
			return $id;
		}

		public function all() {
			return $this->db->query("SELECT * FROM version WHERE is_delete = 0");
		}

		public function modify($ID, $versionName, $versionDescription) {
			$sql = "UPDATE version SET name=?, description=? WHERE ID=?";
			$this->db->query($sql, array($versionName, $versionDescription, $ID));
		}

		public function setCurrent($ID) {
			$this->db->query("UPDATE version SET is_current=0");
			$sql = "UPDATE version SET is_current=1 WHERE ID=?";
			$this->db->query($sql, array($ID));
		}

		public function currentVersion() {
			$query = $this->db->query("SELECT * FROM version where is_current=1");
			if ($query->num_rows() != 1) return "NO CURRENT VERSION!";
			else return $query->row()->name;
		}

		public function getVersion($id) {
			return $this->db->query("SELECT * FROM version WHERE ID=?", array($id));
		}

		public function getResult($vid, $qid) {
			return $this->db->query("SELECT * FROM result WHERE version_id=? AND query_id=?", array($vid, $qid));
		}
	}

?>